<?php
//后台新闻
class PressNavAction extends CommonAction {

    public function index() {
        $pn = D('PressNav');
        parent::search(array(
           'name'=>'栏目名称'
        ));
        if(!isset($_GET['act'])) {
            parent::page($pn->count());
            $allPressNav = $pn->field(array('id', 'name', 'sort', 'info', 'is_active'))
                              ->limit($this->limit)
                              ->order('is_active DESC,sort ASC')
                              ->select();
        } else if($_GET['act'] == 'search') {
            parent::page($pn->where($this->map)->count());
            $allPressNav = $pn->field(array('id', 'name', 'sort', 'info', 'is_active'))
                              ->limit($this->limit)
                              ->where($this->map)
                              ->order('is_active DESC,sort ASC')
                              ->select();
        }
        $this->assign('empty', '<tr><td colspan="6">没有任何数据</td></tr>');
        $this->assign('allpressnav', $allPressNav);
        $this->display();
    }

    public function add() {
        if(isset($_POST['send'])) {
            $pn = D('PressNav');
            if($pn->create()) {
                $pn->add() ? $this->success('新增新闻栏目成功!', __URL__) : $this->error('系统出错，新增新闻栏目失败!', 0);
            } else {
                $this->error($pn->getError());
            }
        }
        $this->display();
    }

    public function update() {
        $id = $this->_get('id');
        if(isset($id)) {
            $pn = D('PressNav');
            if(!$pn->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            if(isset($_POST['send'])) {
                if($pn->create()) {
                    $pn->save() ? $this->success('修改新闻栏目成功!', __URL__) : $this->error('系统出错，修改新闻栏目失败!', 0);
                } else {
                    $this->error($pn->getError(), 0);
                }
            }
            $pressNav = $pn->where('id='.$id)->field(array('id', 'name', 'info', 'is_active'))->find();
            $this->assign('pressnav', $pressNav);
            $this->display();
        }

    }

    public function delete() {
        $id = $this->_get('id');
        if(isset($id)) {
            $mem = parent::useMemcached();
            // 删除对应的mem key
            $mem->delete("navname-$id");
            $pn = D('PressNav');
            if(!$pn->where('id='.$id)->getField('id')) $this->error('不存在此数据!', 0);
            $pn->where('id='.$id)->delete() ? $this->success('删除新闻栏目成功!', __URL__) : $this->error('系统出错，删除新闻栏目失败!', 0);
        }
    }

    public function mulDelete() {
        $ids = implode(',', $_POST['select']);
        if(!$ids) $this->error('请先勾选一个选项!', 0);
        $pn = D('PressNav');
        $map['id'] = array('in', $ids);
        $pn->where($map)->delete() ? $this->success('批量删除新闻栏目成功!', __URL__) : $this->error('系统出错，批量删除新闻栏目失败!', 0);
    }

    public function sort() {
        $infoNav = D('PressNav');
        $idArr = $_POST['sid'];
        foreach ($_POST['sort'] as $key=>$value) {
            $data['id'] = $idArr[$key];
            $data['sort'] = $value;
            $infoNav->save($data);
        }
        $this->success('排序成功!', __URL__);
    }
}

?>